﻿using Avalonia.Controls.Templates;
using Avalonia.Controls;
using Avalonia.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AvaloniaApplication.Core
{
    public class ShapesTemplateSelector : IDataTemplate
    {
        // 用字典来存储图形， 并且标记属性 [Content], 这样待会可以直接用Add添加
        [Content]
        public Dictionary<string, IDataTemplate> AvailableTemplates { get; } = new Dictionary<string, IDataTemplate>();

        // 构建数据模板
        public IControl Build(object param)
        {
            //判断是不是空
            var key = param.ToString(); // Our Keys in the dictionary are strings, so we call .ToString() to get the key to look up
            if (key is null) // If the key is null, we throw an ArgumentNullException
            {
                throw new ArgumentNullException(nameof(param));
            }
            return AvailableTemplates[key].Build(param); // 添加数据模板，然后系统自动构建
        }

        // 检查数据是不是能够录入进来
        public bool Match(object data)
        {
            // Our Keys in the dictionary are strings, so we call .ToString() to get the key to look up
            var key = data.ToString();

            return data is ShapeType// the provided data needs to be our enum type
                   && !string.IsNullOrEmpty(key)           // and the key must not be null or empty
                   && AvailableTemplates.ContainsKey(key); // and the key must be found in our Dictionary
        }
    }
}

